package top.lixunda.ecommerce.server.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.user.api.controller.IUserPermissionController;
import top.lixunda.ecommerce.server.user.api.entity.db.UserPermissionDO;
import top.lixunda.ecommerce.server.user.service.IUserPermissionService;

import java.util.List;
import java.util.Map;

/**
 * 用户权限查询接口
 *
 * @author Lixunda
 * @since 2020-04-24
 */
@Slf4j
@RestController
public class UserPermissionController extends BaseController implements IUserPermissionController {

    private final IUserPermissionService userPermissionService;

    @Autowired
    public UserPermissionController(IUserPermissionService userPermissionService) {
        this.userPermissionService = userPermissionService;
    }

    /**
     * 根据账号查询用户权限
     *
     * @param account 用户账户
     * @return 权限列表
     */
    @Override
    public MessageObject<List<UserPermissionDO>> queryPermissionsByAccount(Integer account) {
        List<UserPermissionDO> result = userPermissionService.queryPermissionsByAccount(account);
        return getSuccessMessageObject("查询用户权限成功！", result);
    }

    /**
     * 根据账号查询用户权限
     *
     * @param account  用户账户
     * @param userType 用户类型
     * @return 权限列表
     */
    @Override
    public MessageObject<List<UserPermissionDO>> queryPermissionsByAccountAndType(
            Integer account, String userType) {
        List<UserPermissionDO> result = userPermissionService.queryPermissionsByAccountAndType(account, userType);
        return getSuccessMessageObject("查询用户权限成功！", result);
    }

    /**
     * 查询当前登陆账户的权限
     *
     * @return 权限列表
     */
    @Override
    public MessageObject<List<UserPermissionDO>> queryCurrentPermissions() {
        List<UserPermissionDO> result = userPermissionService.queryCurrentPermissions();
        return getSuccessMessageObject("查询用户权限成功！", result);
    }

    /**
     * 根据多个用户账户查询多个账户的权限
     *
     * @param accounts 账户列表
     * @return 多个账户的权限列表，键为用户编号，值为权限列表
     */
    @Override
    public MessageObject<Map<Integer, List<UserPermissionDO>>> queryUsersPermissions(
            List<Integer> accounts) {
        Map<Integer, List<UserPermissionDO>> result = userPermissionService.queryUsersPermissions(accounts);
        return getSuccessMessageObject("查询用户权限成功！", result);
    }
}
